package com.dlc.felear.xiaoensale.utils;

import android.content.Context;
import android.media.AudioManager;
import android.media.MediaPlayer;

import com.dlc.felear.xiaoensale.R;

public class DeviceVolumeControl {
    private static String TAG="DeviceVolumeControl";
    private static DeviceVolumeControl instance;
    private AudioManager mAudioManager;
    private int maxVolume,currentVolume;
    private int maxAlarmVolume,currentAlarmVolume;
    private Context context;
    private MediaPlayer mediaPlayer;

    private DeviceVolumeControl(Context context){
        this.context=context;
        mediaPlayer=MediaPlayer.create(context, R.raw.tips_no_order);
        mAudioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
        maxVolume = mAudioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
        maxAlarmVolume = mAudioManager.getStreamMaxVolume(AudioManager.STREAM_ALARM);
    }
    public static DeviceVolumeControl getInstance(Context context){
        if(instance==null){
            instance=new DeviceVolumeControl(context);
        }

        return instance;
    }
    public  int addVolume(){
        int tempp=0;
        currentVolume = mAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
        MyLog.e(TAG,"currentVolume="+currentVolume);
        if(currentVolume>=maxVolume){
            tempp=-1;
        }else {
            int temp=currentVolume+1;
            mAudioManager.setStreamVolume(AudioManager.STREAM_MUSIC, temp, 0);
            tempp=temp;
        }
        mediaPlayer.start();
        return tempp;
    }
    public  int reduceVolume(){
        int tempp=0;
        currentVolume = mAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
        MyLog.e(TAG,"currentVolume="+currentVolume);
       if(currentVolume<=0){
            tempp=-1;
        }else {
            int temp=currentVolume-1;
            if(temp<=0){
                temp=0;
            }
            mAudioManager.setStreamVolume(AudioManager.STREAM_MUSIC, temp, 0);
            tempp=temp;
        }
        mediaPlayer.start();
        return tempp;
    }
    public  int addAlarmVolume(){
        int tempp=0;
        currentAlarmVolume = mAudioManager.getStreamVolume(AudioManager.STREAM_ALARM);
        MyLog.e(TAG,"currentVolume="+currentVolume);
        if(currentAlarmVolume>=maxAlarmVolume){
            tempp=-1;
        }else {
            int temp=currentAlarmVolume+1;
            mAudioManager.setStreamVolume(AudioManager.STREAM_ALARM, temp, 0);
            tempp=temp;
        }
        testTips();
        return tempp;
    }
    public  int reduceAlarmVolume(){
        int tempp=0;
        currentAlarmVolume = mAudioManager.getStreamVolume(AudioManager.STREAM_ALARM);
        MyLog.e(TAG,"currentVolume="+currentVolume);
       if(currentAlarmVolume<=0){
            tempp=-1;
        }else {
            int temp=currentAlarmVolume-1;
            if(temp<=0){
                temp=0;
            }
            mAudioManager.setStreamVolume(AudioManager.STREAM_ALARM, temp, 0);
            tempp=temp;
        }
        testTips();
        return tempp;
    }

    private void testTips(){
        PlayVoiceUtil.getInstance(context).playRfidVoice(R.raw.tips_no_order);
    }
}
